package com.example.travelservlet.managementLogin.service;

import com.example.travelservlet.entity.Manager;
import com.example.travelservlet.managementLogin.dao.LoginRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author kiki
 * @dtae 2023/5/31 15:48
 */
@Service
public class LoginService {
    @Autowired
    private LoginRepository loginRepository;

    public Manager findManagerByUsernameAndPassword(String name, String pwd) {
        return loginRepository.findByUsernameAndPassword(name, pwd);
    }

    public Integer setManagerState(String id) {
        return loginRepository.setManagerStateSignin(id);
    }

    public Manager findManagerById(String userid) {
        Manager manager = loginRepository.findManagerById(userid);
        if (manager == null) {
            return null;
        } else {
            return manager;
        }
    }

    public Integer signout(String userid) {
        return loginRepository.setManagerStateSignout(userid);
    }

    public Boolean editManagerById(String id, String username, String password) {
        Integer integer = loginRepository.editManagerById(id, username, password);
        if (integer == 1) {
            return true;
        } else {
            return false;
        }
    }

    public void deleteById(String userid) {
        loginRepository.deleteById(userid);
    }

    public Boolean addManger(String username, String password) {
        Manager manager = new Manager();
        manager.setId(java.util.UUID.randomUUID().toString().replaceAll("-", ""));
//        List<Manager> managerList = findAllManager();
//        int num = 1;
//        if (managerList != null) {
//            num = managerList.size()+1;
//        }
//        manager.setId(num);
        manager.setState(0);
        manager.setRole("normal");
        manager.setUsername(username);
        manager.setPassword(password);
        Manager save = loginRepository.save(manager);
        if (save != null) {
            return true;
        } else {
            return false;
        }
    }

    public List<Manager> findAllManager() {
        List<Manager> managerList = loginRepository.findAll();
        return managerList;
    }
}
